import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * JDBC操作DML删除语句
 * 删除 Tony 用户
 * DELETE FROM userinfo WHERE username='Tony'
 */
public class JDBCDemo3 {
    public static void main(String[] args) {
        try {
            // 1.加载数据库驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2.创建数据库连接对象
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true","root","root");
            // 3.创建执行对象
            Statement statement = connection.createStatement();
            // 接收用户输入
            Scanner scanner = new Scanner(System.in);
            System.out.println("注销用户功能");
            System.out.println("请输入用户名:");
            String username = scanner.nextLine();

            String sql = "DELETE FROM userinfo WHERE username='"+username+"'";
            int i = statement.executeUpdate(sql);
            if (i>0){
                System.out.println("删除成功");
            }else{
                System.out.println("删除失败");
            }

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }

    }
}



